//
//  ShoppingCartCell.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/28.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "ShoppingCartCell.h"

#import "CartGoodsCell.h"

@interface ShoppingCartCell()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UIView *bgView;

@property (nonatomic,strong) UIButton *selectBtn;

@property (nonatomic,strong) UIImageView *logoImgV;

@property (nonatomic,strong) UILabel *nameLB;

@property (nonatomic,strong) UIButton *toDetailBtn;

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *dataSourceArr;

@property (nonatomic,assign) BOOL allSelect;

@end

@implementation ShoppingCartCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = MainBackColor;
        self.backgroundColor = MainBackColor;
        
        self.dataSourceArr = [NSMutableArray array];
        
        self.allSelect = NO;
        
        [self initUI];
    }
    return self;
}

#pragma mark - setter
- (void)setModel:(ShoppongCartModel *)model{
    _model = model;
    
    self.bgView.height = _model.cellHeight - 12;
    self.tableView.height = _model.cellHeight - 62;
    self.nameLB.text = _model.company;
    self.nameLB.width = [Configuration getLabelWidthWithText:self.nameLB.text withFont:self.nameLB.font];
    self.toDetailBtn.left = self.nameLB.right + 5;
    
    self.dataSourceArr = [NSMutableArray arrayWithArray:_model.products];
    
    NSInteger count = 0;
    for (CartGoodsModel *model in self.dataSourceArr) {
        if (model.isSelect == NO) {
            count = count + 1;
        }
    }
    [self.selectBtn setImage:count <= 0 ? LoadImage(@"购物车_选中"):LoadImage(@"购物车_未选中") forState:normal];
    
    self.allSelect = count <= 0 ? YES:NO;
    
    [self.tableView reloadData];
}

#pragma mark - UI
-(void)initUI{
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.selectBtn];
    [self.bgView addSubview:self.logoImgV];
    [self.bgView addSubview:self.nameLB];
    [self.bgView addSubview:self.toDetailBtn];
    [self.bgView addSubview:self.tableView];
}

#pragma mark - lazy
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, ScreenWidth-20, 160)];
        _bgView.clipsToBounds = YES;
        _bgView.layer.cornerRadius = 8;
        _bgView.backgroundColor = UIColor.whiteColor;
    }
    return _bgView;
}
- (UIButton *)selectBtn{
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _selectBtn.frame = CGRectMake(13, 17, 16, 16);
        [_selectBtn setImage:LoadImage(@"购物车_未选中") forState:normal];
        [_selectBtn addTarget:self action:@selector(selectGoodsClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

- (UIImageView *)logoImgV{
    if (!_logoImgV) {
        _logoImgV = [[UIImageView alloc] initWithFrame:CGRectMake(self.selectBtn.right + 12, self.selectBtn.top, 15, 15)];
        _logoImgV.image = LoadImage(@"商品详情_店铺");
    }
    return _logoImgV;
}

- (UILabel *)nameLB{
    if (!_nameLB) {
        _nameLB = [[UILabel alloc] initWithFrame:CGRectMake(self.logoImgV.right + 4, self.logoImgV.top - 2, 80, 20)];
        _nameLB.textColor = MainBlackTextColor;
        _nameLB.font = FontSet(15);
        _nameLB.textAlignment = NSTextAlignmentLeft;
        
        _nameLB.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toStoreClick:)];
        tap.delegate = self;
        [_nameLB addGestureRecognizer:tap];
    }
    return _nameLB;
}

- (UIButton *)toDetailBtn{
    if (!_toDetailBtn) {
        _toDetailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _toDetailBtn.frame = CGRectMake(self.nameLB.right + 5, self.nameLB.top, 40, 20);
        [_toDetailBtn setImage:LoadImage(@"个人中心_查看全部") forState:normal];
        [_toDetailBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 0, 5, 34)];
        [_toDetailBtn addTarget:self action:@selector(toDetailClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _toDetailBtn;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 50, ScreenWidth-20, 110) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = MainBackColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.scrollEnabled = NO;
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.1f)];
        [_tableView setTableHeaderView:view];
    }
    return _tableView;
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CartGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CartGoodsCell"];
    if (!cell) {
        cell = [[CartGoodsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CartGoodsCell"];
    }
    cell.model = self.dataSourceArr[indexPath.row];
    
    [cell setUpdateModelStatusBlock:^(CartGoodsModel * _Nonnull goodsM) {
        [self updateGoodsSelectStatus:goodsM];
    }];
    
    return cell;
}

#pragma mark - SEL
-(void)selectGoodsClick{
    self.allSelect = !self.allSelect;
    
    [self.selectBtn setImage:self.allSelect == YES ? LoadImage(@"购物车_选中"):LoadImage(@"购物车_未选中") forState:normal];
    
    if (self.allSelect == YES) {
        for (int i = 0; i<self.dataSourceArr.count; i++) {
            CartGoodsModel *cartM = self.dataSourceArr[i];
            cartM.isSelect = YES;
            [self.dataSourceArr replaceObjectAtIndex:i withObject:cartM];
        }
    }else{
        for (int i = 0; i<self.dataSourceArr.count; i++) {
            CartGoodsModel *cartM = self.dataSourceArr[i];
            cartM.isSelect = NO;
            [self.dataSourceArr replaceObjectAtIndex:i withObject:cartM];
        }
    }
    [self.tableView reloadData];
    
    self.model.products = (NSArray *)self.dataSourceArr;
    
    if (self.updateShopStatusBlock) {
        self.updateShopStatusBlock(self.model);
    }
}

//进入店铺
-(void)toStoreClick:(UIGestureRecognizer *)tap{
    [self toDetailClick];
}
//进入店铺
-(void)toDetailClick{
    
}

//更新商品选中状态
-(void)updateGoodsSelectStatus:(CartGoodsModel *)model{
    for (int i = 0; i<self.dataSourceArr.count; i++) {
        CartGoodsModel *cartM = self.dataSourceArr[i];
        if (cartM.skuId == model.skuId) {
            [self.dataSourceArr replaceObjectAtIndex:i withObject:model];
        }
    }
    self.model.products = (NSArray *)self.dataSourceArr;
    if (self.updateShopStatusBlock) {
        self.updateShopStatusBlock(self.model);
    }
    
    static NSInteger count = 0;
    for (CartGoodsModel *model in self.dataSourceArr) {
        if (model.isSelect == NO) {
            count = count + 1;
        }
    }
    [self.selectBtn setImage:count <= 0 ? LoadImage(@"购物车_选中"):LoadImage(@"购物车_未选中") forState:normal];
    self.allSelect = count <= 0 ? YES:NO;
}



@end
